﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex5.InsertionSort
{
    public class InsertionSortAlgorithm
    {
        private int[] internalArray;
        private int numberOfElements;

        public int[] Array { get { return this.internalArray; } }
        public void InsertionSort()
        {
            int i;
            int j;
            int index;

            for (i = 1; i < numberOfElements; i++)
            {
                index = internalArray[i];
                j = i;

                while ((j > 0) && (internalArray[j - 1] > index))
                {
                    internalArray[j] = internalArray[j - 1];
                    j = j - 1;
                }
                internalArray[j] = index;
            }
        }

        public InsertionSortAlgorithm(int[] array)
        {
            if (array == null)
            {
                throw new ArgumentNullException("Array can't be null!");
            }
            this.internalArray = array;
            numberOfElements = array.Length;
        }
    }
}
